<template>
  <div class="login">
    <div class="wz_div">
      <h2 class="h5_text">在线教育网后台管理系统</h2>
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        class="demo-ruleForm"
      >
        <el-form-item  prop="email">
          <el-input
            v-model="ruleForm.email"
            class="emil"
            prefix-icon="el-icon-user-solid"
            placeholder="请输入邮箱"
          ></el-input>
        </el-form-item>
        <el-form-item  prop="password">
          <el-input
            v-model="ruleForm.password"
            prefix-icon="el-icon-lock"
            placeholder="请输入密码"
            type="password"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')"
            >登录</el-button
          >
        </el-form-item>
      </el-form>

      <p class="p_text">
        您没有账户？<span @click="enroll" class="enroll">点击注册</span>
      </p>
    </div>
  </div>
</template>
<script>
import { loginApi } from "@/utils/home.js";
export default {
  data() {
    return {
      ruleForm: {
        email: "",
        password: "",
      },
      rules: {
        email: [
          { required: true, message: "您需要输入用户名", trigger: "blur" },
          { min: 5, max: 20, message: "长度在5-20个之间", trigger: "change" },
        ],
        password: [
          { required: true, message: "您需要输入密码", trigger: "blur" },
          { min: 5, max: 20, message: "长度在5-20个之间", trigger: "change" },
        ],
      },
    };
  },
  methods: {
    //注册
    enroll() {
      this.$router.push("/regist");
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // 注册成功
          loginApi(this.ruleForm).then((res) => {
            console.log(res);
            if (res.success == true) {
              //   登录成功
              // 把后端返回的token存在本地，以后可以判断本地有没有token来判断这个用户有没有登录
              localStorage.setItem("token", res.token);
              this.$router.push('/welcome')
            } else {
                alert('错误')
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>
<style lang="scss" scoped>

.login {
  background-color: #2d3a4b;
  width: 100%;
  height: 100vh;
  // display: flex;
  // justify-content: center;
  // align-items: center;
  .wz_div {
    // margin-top: 240px;
    position: fixed;
    left: 50%;
    top: 50%;
    transform: translate(-50%,-50%);
    width: 500px;

    .h5_text {
      color: #fff;
      text-align: center;
      margin-bottom: 18px;
      // display: flex;
      // justify-content: center;
      // align-items: center;
      // margin-left: 70px;
    }

    .p_text {
      text-align: right;
      margin-top: 26px;
      color: #fff;
      .enroll {
        cursor: pointer;
      }
    }
  }
}
::v-deep .el-button--primary {
  width: 100%;
}
</style>